#!/usr/bin/python
import cherrypy
import csv
#coordenada?lati=<latitude>&long=<longitude>
#  Retorna os pontos mais proximos de long/lati
#bus_stop/<numero_ponto>
#  Retorna as linhas que passam pelo ponto
#bus_stop/<numero_ponto>/<numero_ponto1>
#  Retorna as linhas que passam pelos pontos
class Coordenada(object):

    def __init__(self):
        self.content = None

    exposed = True

    @cherrypy.tools.json_out()
    def GET(self, *path, **args):
        if len(path) == 0:
            user_long = args["long"]
            user_lati = args["lati"]

            return "{" + user_long + "," + user_lati + "}"
        else:
            if path[0] == "lista":
                file = open('coord.csv')
                data = list(csv.reader(file))
                return data

            elif path[0] == "inserir":
                new_coord = [args["lati"], args["long"]]

                file = open('coord.csv', 'ab')

                csvfile = csv.writer(file)

                csvfile.writerow(new_coord)

                return new_coord

            elif path[0] == 'limpar':
                file = open('coord.csv', 'wb')
                return '0'

            return "1";

class Root(object):
    pass

root = Root()

root.coordenada = Coordenada()

conf = {
    'global': {
        'server.socket_host': '0.0.0.0',
        'server.socket_port': 8009,
    },
    '/': {
        'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
    }
}

cherrypy.quickstart(root, '/', conf)
